package com.meituan.catering.management.product.biz.model.converter;

import com.meituan.catering.management.common.model.api.http.AuditingHttpModel;
import com.meituan.catering.management.common.model.biz.AuditingBO;
import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.product.api.http.model.dto.ProductPageHttpDTO;
import com.meituan.catering.management.product.biz.model.ProductBO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Believe
 * Date 2022/5/19 16:18
 */
public class ProductPageHttpDTOConverter {
    public static ProductPageHttpDTO toProductPageHttpResponse(PageBO<ProductBO> pageBO) {
        ProductPageHttpDTO productPageHttpDTO = new ProductPageHttpDTO();
        productPageHttpDTO.setPageIndex(pageBO.getPageIndex());
        productPageHttpDTO.setPageSize(pageBO.getPageSize());
        productPageHttpDTO.setTotalCount(pageBO.getTotalCount());
        productPageHttpDTO.setTotalPageCount(pageBO.getTotalPageCount());
        productPageHttpDTO.setRecords(toRecord(pageBO.getRecords()));
        return productPageHttpDTO;
    }

    private static List<ProductPageHttpDTO.Record> toRecord(List<ProductBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<ProductPageHttpDTO.Record> recordList = new ArrayList<>();
        for (ProductBO productBO : list) {
            ProductPageHttpDTO.Record record = new ProductPageHttpDTO.Record();
            record.setId(productBO.getId());
            record.setTenantId(productBO.getTenantId());
            record.setVersion(productBO.getVersion());
            record.setAuditing(toAuditingHttpModel(productBO.getAuditing()));
            record.setName(productBO.getName());
            record.setUnitPrice(productBO.getUnitPrice());
            record.setUnitOfMeasure(productBO.getUnitOfMeasure());
            record.setMinSalesQuantity(productBO.getMinSalesQuantity());
            record.setIncreaseSalesQuantity(productBO.getIncreaseSalesQuantity());
            record.setDescription(productBO.getDescription());
            record.setEnabled(productBO.getEnabled());
            recordList.add(record);
        }
        return recordList;
    }

    private static AuditingHttpModel toAuditingHttpModel(AuditingBO auditing) {
        AuditingHttpModel auditingHttpModel = new AuditingHttpModel();
        auditingHttpModel.setCreatedBy(auditing.getCreatedBy());
        auditingHttpModel.setCreatedAt(auditing.getCreatedAt());
        auditingHttpModel.setLastModifiedBy(auditing.getLastModifiedBy());
        auditingHttpModel.setLastModifiedAt(auditing.getLastModifiedAt());
        return auditingHttpModel;
    }
}
